using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_SpotManagement : admin_BaseUserControl
{
    SpotDAO daoSpot = new SpotDAO();
    Spot sp = new Spot();
    int id = 0;
    #region properties
    /// <summary>
    /// Return image URL of the Spot object
    /// </summary>
    public string ImageURL
    {
        get { return sp.ImageURL; }
    }
    public string ID
    {
        get { return sp.Id.ToString(); }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
        ActiveAccording = "spot";
        if (Request["mode"] == "1")
        {
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
            try
            {
                id = int.Parse(Request["id"]);
                CurrentID = id.ToString();//save current id for updating or deleting
                sp = daoSpot.GetSpot(id);
                txtName.Text = sp.Name;
                txtDescription.Text = sp.Description;
                txtRank.Text = sp.Rank.ToString();
                txtTag.Text = sp.Tag;
                chkIsRecommended.Checked = sp.IsRecommended;
                ckhState.Checked = sp.State;
                fckArticle.Value = sp.Article;

                int index = 0; //reset index
                foreach (ListItem item in cmbCategory.Items)
                {
                    if (item.Value == sp.Category.Id.ToString())
                    {
                        cmbCategory.SelectedIndex = index;
                        break;
                    }
                    index++;
                }
                index = 0; //reset index
                foreach (ListItem item in cmbLocation.Items)
                {
                    if (item.Value == sp.Location.Id.ToString())
                    {
                        cmbLocation.SelectedIndex = index;
                        break;
                    }
                    index++;
                }

            }
            catch (Exception ex)
            {
                Response.Redirect("Default.aspx?ctrl=spot_cate_list");
            }
        }
        else
        {
            txtName.Text = "";
            txtDescription.Text = "";
            txtRank.Text = "0";
            txtTag.Text = "";
            fckArticle.Value = "";
            
            lnkAdd.Visible = true;
            lnkUpdate.Visible = false;
            lnkDelete.Visible = false;

        }
    }
    /// <summary>
    /// Load initialize data
    /// </summary>
    public void LoadData()
    {
        SpotCategoryDAO daoSpotCate = new SpotCategoryDAO();
        LocationDAO daoLocation = new LocationDAO();
        cmbCategory.DataSource = daoSpotCate.GetAllSpotCategories("");
        cmbCategory.DataValueField = "Id";
        cmbCategory.DataTextField = "Name";
        cmbCategory.DataBind();
        cmbLocation.DataSource = daoLocation.GetAllLocations();
        cmbLocation.DataValueField = "Id";
        cmbLocation.DataTextField = "Name";
        cmbLocation.DataBind();
        cmbCategory.SelectedIndex = 0;
        cmbLocation.SelectedIndex = 0;
    }

    protected void lnkAdd_Click(object sender, EventArgs e)
    {
         sp = new Spot();
        sp.Name = txtName.Text;
        sp.Description = txtDescription.Text;
        sp.Category.Id = int.Parse(cmbCategory.SelectedValue);
        sp.Location.Id = int.Parse(cmbLocation.SelectedValue);
        sp.ImageURL=Request["txtImage"];
        sp.Article = fckArticle.Value;
        sp.IsRecommended = chkIsRecommended.Checked;
        sp.State = ckhState.Checked;
        sp.ModifiedDate = DateTime.Now;
        sp.ModifiedBy = 1;
        sp.ImageURL=Request["txtImage"];
        if (daoSpot.Insert(sp) > 0)
        {
            ShowMessage("New spot is created");
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
            CurrentID = sp.Id.ToString();
        }
        else ShowMessage("Error occured when trying to create new spot. Action failed.");
    }
    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        if (id == 0)
        {
            try
            {
                id = int.Parse(CurrentID);
                sp.Id = id;
            }
            catch (Exception ex)
            {
                ShowMessage("Error occured when trying to update spot information");
            }

        }
        sp.Name = txtName.Text;
        sp.Description = txtDescription.Text;
        sp.Category.Id = int.Parse(cmbCategory.SelectedValue);
        sp.Location.Id = int.Parse(cmbLocation.SelectedValue);
        sp.ImageURL = Request["txtImage"];
        sp.Article = fckArticle.Value;
        sp.IsRecommended = chkIsRecommended.Checked;
        sp.State = ckhState.Checked;
        sp.ModifiedDate = DateTime.Now;
        sp.ModifiedBy = 1;
        if (daoSpot.Update(sp) > 0)
        {
            ShowMessage("Spot is updated");
        }
        else ShowMessage("Error occured when trying to update the spot's information. Action failed.");
    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        id = int.Parse(CurrentID);
        if (daoSpot.Delete(id) > 0)
        {
            ShowMessage("Spot information is deleted");
        }
        else ShowMessage("Error occured. Action failed.");
    }
    protected void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void cmbLocation_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void chkIsRecommended_CheckedChanged(object sender, EventArgs e)
    {

    }
}
